package cn.com.icbc.wjl.GWT.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class TaskFrom implements EntryPoint {

	private TextBox titleField;
	private Label titleErrorLabel;
	private TextArea descriptionField;
	private Label descriptionErrorLabel;
	private ListBox priorityField;
	
	public void onModuleLoad() {
		this.titleField = new TextBox();
		this.titleErrorLabel = createErrorLabel();
		RootPanel.get("titleLabel").add(new Label("Title"));
		RootPanel.get("titleField").add(new TextBox());
		RootPanel.get("titleErrorLabel").add(titleErrorLabel);
		
		this.priorityField = new ListBox(false);
		this.priorityField.setVisibleItemCount(1);
		this.priorityField.addItem("LOW");
		this.priorityField.addItem("NORMAL");
		this.priorityField.addItem("HIGH");
		this.priorityField.setItemSelected(1, true);
		RootPanel.get("priorityLabel").add(new Label("Priority"));
		RootPanel.get("priorityField").add(priorityField);
		
		
		this.descriptionField = new TextArea();
		descriptionField.setVisibleLines(10);
		descriptionField.setCharacterWidth(50);
		this.descriptionErrorLabel = new Label();
		RootPanel.get("descriptionLabel").add(new Label("Description"));
		RootPanel.get("descriptionField").add(descriptionField);
		RootPanel.get("descriptionErrorLabel").add(descriptionErrorLabel);
		
		Button submitButton = new Button("Add Task");
		RootPanel.get("submitButton").add(submitButton);
		submitButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				if (validateForm()) {
					TaskInfo task = new TaskInfo();
					task.setTitle(titleField.getText());
					task.setDescription(descriptionField.getText());
					TaskInfo.Priority priority = resolvePriority();
					task.setPriority(priority);
					addTask(task);
				}
			}


		});
		
	}
	protected TaskInfo.Priority resolvePriority() {
		// TODO Auto-generated method stub
		int priorityIndex = this.priorityField.getSelectedIndex();
		String priorityName = this.priorityField.getValue(priorityIndex);
		return Enum.valueOf(TaskInfo.Priority.class, priorityName);
	}
	protected void addTask(TaskInfo task) {
		// TODO Auto-generated method stub
		Window.alert(task.toString());
	}

	protected boolean validateForm() {
		// TODO Auto-generated method stub
		boolean titleIsValid = titleField.getText().length() > 0;
		this.titleErrorLabel.setText(titleIsValid ? "" : "Requied");
		return titleIsValid;
	}

	private Label createErrorLabel() {
		// TODO Auto-generated method stub
		Label errorLabel = new Label();
		errorLabel.addStyleName("errorLabel");
		return errorLabel;
	}

}
